package com.mcxx.util;

import cn.hutool.core.date.DateUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.utils.StringUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Date;


public class FileExportUtil {

    public static String defaultContentType = "application/octet-stream";
    public static String defaultCharset = "UTF-8";


    public static void exportFileResponse(HttpServletResponse response, ExcelInfo excelInfo, String areaName) throws IOException {
        byte[] file = excelInfo.getFile();
        String fileName = excelInfo.getFileName();
        String[] fileInfo = fileName.split("\\.");
        StringBuffer lastFileName = new StringBuffer(areaName);
        lastFileName.append(fileInfo[0]).append(DateUtil.format(new Date(), "yyyyMMdd"))
                .append(".").append(fileInfo[1]);
        InputStream sbs = new ByteArrayInputStream(file);
        Throwable var3 = null;

        try {
            exportFileWithStream(response, defaultContentType, sbs, lastFileName.toString());
        } catch (Throwable var12) {
            var3 = var12;
            throw var12;
        } finally {
            if (sbs != null) {
                if (var3 != null) {
                    try {
                        sbs.close();
                    } catch (Throwable var11) {
                        var3.addSuppressed(var11);
                    }
                } else {
                    sbs.close();
                }
            }

        }
    }


    public static void exportFileWithStream(HttpServletResponse response, String contentType, InputStream inputStream, String fileName) throws IOException {
        OutputStream outputStream = response.getOutputStream();
        initResponse(response, contentType, fileName);
        IOUtils.copy(inputStream, outputStream);
        outputStream.flush();
    }


    private static void initResponse(HttpServletResponse response, String contentType, String fileName) throws IOException {
        if (StringUtils.isBlank(fileName)) {
            fileName = "export";
        }

        response.setContentType(contentType + "; charset=" + defaultCharset);
        fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), "ISO8859-1");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setHeader("Access-Control-Expose-Headers", "filename");
        response.setHeader("filename", fileName);
    }


    public static void downLoadTemplate(HttpServletResponse response, byte[] byt, String areaName,String fileName) throws IOException {
        String[] fileInfo = fileName.split("\\.");
        StringBuffer lastFileName = new StringBuffer(areaName);
        lastFileName.append(fileInfo[0]).append(DateUtil.format(new Date(), "yyyyMMdd"))
                .append(".").append(fileInfo[1]);
        InputStream sbs = new ByteArrayInputStream(byt);
        Throwable var3 = null;

        try {
            exportFileWithStream(response, defaultContentType, sbs, lastFileName.toString());
        } catch (Throwable var12) {
            var3 = var12;
            throw var12;
        } finally {
            if (sbs != null) {
                if (var3 != null) {
                    try {
                        sbs.close();
                    } catch (Throwable var11) {
                        var3.addSuppressed(var11);
                    }
                } else {
                    sbs.close();
                }
            }

        }
    }
}
